// pages/park/park.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 31.648640362470292,
    longitude: 117.93029931895853,
    markers: [{
      id: 0,
      name: "当前位置",
      info: {
        title: "",
        total: 100,
        price: "免费",
        address: "当前位置"
      },
      latitude: 31.648640362470292,
      longitude: 117.93029931895853,
      width: "60rpx",
      height: "60rpx",
      iconPath: '../../images/bar/dingwei_active.png',
      customCallout: {
        anchorY: 0,
        anchorX: 0,
        display: 'ALWAYS'
      },
    }],
    customCalloutMarkerIds: [],
    num: 1,
    tabs: [],
    scale: 16,
    activeTab: 0,
    markInfoList: [{
      title: "图书馆地下停车场",
      total: 100,
      price: "免费",
      address: "巢湖学院图书馆"
    }, {
      title: "博学楼室外停车场",
      total: 30,
      price: "免费",
      address: "巢湖学院博学楼外"
    }, {
      title: "第三食堂室外停车场",
      total: 20,
      price: "免费",
      address: "巢湖学院第三学生食堂外"
    }, {
      title: "第二食堂室外停车场",
      total: 30,
      price: "免费",
      address: "巢湖学院第二食堂外"
    }, {
      title: "后山停车场",
      total: 30,
      price: "免费",
      address: "巢湖学院后山"
    }, ]
  },
  onTabClick(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },
  // 点击了全部
  ChangeToall() {
    const allmarkers = [this.data.markers[0], {
      id: 1,
      name: "图书馆地下停车场",
      info: {
        title: "",
        total: 100,
        price: "免费",
        address: "巢湖学院图书馆"
      },
      latitude: 31.645933148596523,
      longitude: 117.93032973401682,
      width: "60rpx",
      height: "60rpx",
      iconPath: '../../images/park.png',
      customCallout: {
        anchorY: 0,
        anchorX: 0,
        display: 'ALWAYS'
      },
    }, {
      id: 2,
      name: "博学楼室外停车场",
      info: {
        title: "博学楼室外停车场",
        total: 30,
        price: "免费",
        address: "巢湖学院博学楼外"
      },
      latitude: 31.648799968008305,
      longitude: 117.92763044201934,
      iconPath: '../../images/park.png',
      width: "60rpx",
      height: "60rpx",
      customCallout: {
        anchorY: 10,
        anchorX: 0,
        display: 'ALWAYS',
      },
    }, {
      id: 3,
      name: "第三食堂室外停车场",
      info: {
        title: "第三食堂室外停车场",
        total: 20,
        price: "免费",
        address: "巢湖学院第三学生食堂外"
      },
      latitude: 31.64783366456766,
      longitude: 117.93454129893144,
      width: "60rpx",
      height: "60rpx",
      iconPath: '../../images/park.png',
      customCallout: {
        anchorY: 0,
        anchorX: 20,
        display: 'ALWAYS',
      },
    }, {
      id: 4,
      name: "第二食堂室外停车场",
      info: {
        title: "第二食堂室外停车场",
        total: 30,
        price: "免费",
        address: "巢湖学院第二食堂外"
      },
      latitude: 31.65087568858621,
      longitude: 117.93136530747347,
      iconPath: '../../images/park.png',
      width: "60rpx",
      height: "60rpx",
      customCallout: {
        anchorY: 10,
        anchorX: 0,
        display: 'ALWAYS',
      },
    }, {
      id: 5,
      name: "后山停车场",
      info: {
        title: "后山停车场",
        total: 30,
        price: "免费",
        address: "巢湖学院后山"
      },
      latitude: 31.6519825759116,
      longitude: 117.92833561564134,
      iconPath: '../../images/park.png',
      width: "60rpx",
      height: "60rpx",
      customCallout: {
        anchorY: 10,
        anchorX: 0,
        display: 'ALWAYS',
      },
    }]
    this.setData({
      markers: allmarkers,
      customCalloutMarkerIds: [0, 1, 2, 3, 4, 5]
    })
  },
  // 放大当前点击的位置
  tapToPark(index) {
    this.setData({
      latitude: this.data.markers[index].latitude,
      longitude: this.data.markers[index].longitude,
      scale: 19,
    })
    console.log("完成了放大");
  },
  // 文字点击事件
  callouttap(e) {
    const markerIndex = e.detail.markerId;
    this.tapToPark(markerIndex)
  },
  // 图标点击
  markertap(e) {
    const markerIndex = e.detail.markerId;
    this.tapToPark(markerIndex)
  },
  // 列表点击
  parkListItemtap(e) {
    console.log(e);
    const markindex = e.currentTarget.dataset.id + 1;
    this.tapToPark(markindex)
  },
  // 地图点击事件
  maptap(e) {
    console.log("地图点击:", e);
  },
  onChange(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },
  // 获取当前用户的的位置信息，渲染到地图
  GetUserPlace() {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude,
          'markers[0].latitude': latitude,
          'markers[0].longitude': longitude,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.GetUserPlace()
    this.ChangeToall()
    const tabs = [{
        title: '全部',
      },
      {
        title: '公共场库',
      },
      {
        title: '道路停车',
      },
      {
        title: '空余泊位数',
      },
    ]
    this.setData({
      tabs
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})